package com.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @ClassName UseCommand
 * @Description TODO 通过命令调用本机的应用程序
 * @Author ZhangY
 * @Date 2020/1/21 16:47
 * @Version 1.0.0
 */
public class UseCommand {
    public static void main(String[] args) throws IOException {
        executeCommand("C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQScLauncher.exe");
//        executeCommand("ipconfig");
    }

    /**
     * @MethodName executeCommand
     * @author ZhangY 2020/1/21 16:57
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 通过程序启动QQ
     */
    public static void executeCommand(String cmd) {
        BufferedReader reader = null;
        BufferedReader reader1 = null;
        try {
            //执行exe  cmd可以为字符串(exe存放路径)也可为数组，调用exe时需要传入参数时，可以传数组调用(参数有顺序要求)
            Process process = Runtime.getRuntime().exec(cmd);
            String line = null;
            reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
            reader1 = new BufferedReader(new InputStreamReader(process.getErrorStream(),"GBK"));
            while ((line = reader.readLine()) != null  || (line = reader1.readLine()) != null) {
                //输出exe输出的信息以及错误信息
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
